<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/jsp/declare.jsp" %>

<!DOCTYPE html>
<html>
<head>
	<base href="<%=basePath %>" />
	<title>人员-编辑</title>
	<%@ include file="/common/jsp/common.jsp" %>
	
	<script>
	function validate(form) {
		var success = true;
		$(form).find("input").each(function(){
			if (success && !$(this).val()) {
				var _input = this;
				jAlert("请填写完整信息", function(){
					_input.focus();
				});
				success = false;
			}
		});
		return success;
	}

	var roomList = [];
	function loadRoom() {
		doAjax({
			url: addFromUserName("mvc/master/mainRoomGrantList.json"),
			async: false,
			success: function(json){
				if (json && json.length) {
					roomList = json;

					var selector = $('<div id="roomList" class="full-screen-cover"></div>');
					var selectList = $('<div class="selectList"></div>');
					var buttonBar = $('<div class="buttomBar"><input type="button" value="确定" class="submit" /></div>');
					selector.append(selectList).append(buttonBar);
					
					for (var i = 0; i < roomList.length; i++) {
						for (var j = 0; j < 3; j++)
						selectList.append('<label class="bg-white"><input type="radio" name="room_id" value="' + roomList[i].id + '" class="fl" />'
								+ roomList[i].community_name + ' ' + roomList[i].dept_name + ' ' + roomList[i].building + ' '
								+ roomList[i].unit + '单元' + roomList[i].room_num + '房</label>');
					}
					selectList.find("input[name='room_id'][value='${room.id }']").attr("checked", true);
					buttonBar.find("input:button").click(function(){
						var selectId = $("input[name='room_id']:checked").val();
						// 选中值
						$("[name='roomId']").val(selectId);
						// 显示文字
						for (var i = 0; i < roomList.length; i++) {
							if (roomList[i].id == selectId) {
								$("#roomAddress").html(roomList[i].community_name + ' ' + roomList[i].dept_name + ' ' + roomList[i].building + ' '
										+ roomList[i].unit + '单元' + roomList[i].room_num + '房');
								break;
							}
						}
						$("#roomList").hide();
					});
					
					$("body").append(selector);
				}
			}
		});
	}
	function showRoomListToSelect() {
		if (!$("#roomList").size()) {
			loadRoom();
		} else {
			$("#roomList").show();
		}
	}

	function userDel(renterId) {
		jConfirm("确认删除？", function(yes){
			if (!yes)
				return;
			
			redirect("mvc/master/grantDel.htm?renterId=" + renterId + "&communityId=${communityId}&roomId=${roomId}");
		});
	}
	</script>
</head>
<body id="room" class="bg-grey">
	<form action="mvc/master/${communityId}/${renter.id}/grantUpd.htm" method="post" onsubmit="return validate(this);">
		<input type="hidden" name="fromUserName" value="${param.fromUserName }" />
		<input type="hidden" name="roomId" value="${room.id }" />
		<div class="frame">
			<div class="row">
				<div id="roomAddress" class="input" placeholder="房间地址">
					${room.community_name } ${room.dept_name } ${room.building }#${room.unit }单元${room.room_num }房
				</div>
			</div>
			<div class="row">
				<input name="endDate" value="${fn:substring(renter.end_date,0,10)}" class="input" placeholder="截止日期" />
			</div>
		</div>
		<div class="button-bar center">
			<input type="button" value="选择房间" class="submit" onclick="showRoomListToSelect()" />
			<input type="submit" value="保存" class="submit" />
			<input type="button" value="删除" class="delete" onclick="userDel('${renter.id}')" />
		</div>
	</form>
</body>
</html>